import Ability from '@ohos.application.Ability'

const TAG: string = "BtConnector[MainAbility]";

export default class MainAbility extends Ability {
  async onCreate(want, launchParam) {
    console.info(`${TAG} onCreate`)
  }

  onWindowStageCreate(windowStage) {
    // Main window is created, set main page for this ability
    console.info(`${TAG} onWindowStageCreate`)
    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        console.info(`${TAG} onWindowStageCreate ERROR ` + JSON.stringify(err));
        return;
      }
      console.info(`${TAG} onWindowStageCreate data ` + JSON.stringify(data));
    });
    //this.grantPermission()
  }

  grantPermission() {
    console.info(`${TAG} grantPermission`)
    if (this.context !== undefined) {

      this.context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], function (result) {
        console.log(`${TAG} grantPermission,requestPermissionsFromUser,result.requestCode=${result}`)
      })
    }
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    console.info(`${TAG} onWindowStageDestroy`)
  }

  onForeground() {
    // Ability has brought to foreground
    console.info(`${TAG} onForeground`)
  }

  onBackground() {
    // Ability has back to background
    console.info(`${TAG} onBackground`)
  }

  onDestroy() {
    console.info(`${TAG} onDestroy`)
  }
};
